/*
 * CouponAndDiscountDialog.java
 *
 * Created on September 23, 2007, 1:59 PM
 */
package com.floreantpos.ui.dialog;

import java.awt.Component;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.AbstractListModel;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JList;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import com.floreantpos.PosException;
import com.floreantpos.main.Application;
import com.floreantpos.model.CouponAndDiscount;
import com.floreantpos.model.Ticket;
import com.floreantpos.model.TicketCouponAndDiscount;
import com.floreantpos.model.dao.CouponAndDiscountDAO;
import java.text.NumberFormat;
import java.util.Locale;

/**
 *
 * @author mahbub.shahriar
 */
public class CouponAndDiscountDialog extends POSDialog implements ActionListener, ListSelectionListener {

  private List<CouponAndDiscount> couponList;
  private TicketCouponAndDiscount ticketCoupon;
  private Ticket ticket;

  NumberFormat numberFormat = NumberFormat.getInstance(new Locale("pt", "BR"));

  /**
   * Creates new form CouponAndDiscountDialog
   */
  public CouponAndDiscountDialog() {
    super(Application.getPosWindow(), true, false);

    initComponents();

    tfValue.getDocument().addDocumentListener(new DocumentListener() {

      public void insertUpdate(DocumentEvent e) {
        try {
          double totalDiscount = numberFormat.parse(tfValue.getText()).doubleValue();
          lblTotalDiscount.setText(Application.formatNumber(totalDiscount));
        } catch (Exception x) {
        }
      }

      public void removeUpdate(DocumentEvent e) {
        try {
          double totalDiscount = numberFormat.parse(tfValue.getText()).doubleValue();
          lblTotalDiscount.setText(Application.formatNumber(totalDiscount));
        } catch (Exception x) {
        }
      }

      public void changedUpdate(DocumentEvent e) {
        try {
          double totalDiscount = numberFormat.parse(tfValue.getText()).doubleValue();
          lblTotalDiscount.setText(Application.formatNumber(totalDiscount));
        } catch (Exception x) {
        }
      }

    });
    lblTotalDiscount.setText("");
    btnEditValue.setEnabled(false);

    btnUp.setActionCommand("scrollUP");
    btnDown.setActionCommand("scrollDown");
    btnUp.addActionListener(this);
    btnDown.addActionListener(this);
    listCoupons.addListSelectionListener(this);

    listCoupons.setCellRenderer(new CouponListRenderer());

    ticketCoupon = new TicketCouponAndDiscount();
  }

  private void initComponents() {

    titlePanel1 = new com.floreantpos.ui.TitlePanel();
    jScrollPane1 = new javax.swing.JScrollPane();
    listCoupons = new javax.swing.JList();
    btnCancel = new com.floreantpos.swing.PosButton();
    btnOk = new com.floreantpos.swing.PosButton();
    jSeparator1 = new javax.swing.JSeparator();
    btnUp = new com.floreantpos.swing.PosButton();
    btnDown = new com.floreantpos.swing.PosButton();
    jSeparator2 = new javax.swing.JSeparator();
    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    jLabel3 = new javax.swing.JLabel();
    jLabel4 = new javax.swing.JLabel();
    tfName = new javax.swing.JTextField();
    tfNumber = new javax.swing.JTextField();
    tfType = new javax.swing.JTextField();
    tfValue = new javax.swing.JTextField();
    btnEditValue = new javax.swing.JButton();
    jSeparator3 = new javax.swing.JSeparator();
    jLabel5 = new javax.swing.JLabel();
    lblTotalDiscount = new javax.swing.JLabel();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

    titlePanel1.setTitle(com.floreantpos.POSConstants.SELECT_DISCOUNT);

    jScrollPane1.setViewportView(listCoupons);

    btnCancel.setText(com.floreantpos.POSConstants.CAPITAL_VOID);
    btnCancel.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        doCancel(evt);
      }
    });

    btnOk.setText(com.floreantpos.POSConstants.OK);
    btnOk.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        doOk(evt);
      }
    });

    btnUp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/up_32.png")));

    btnDown.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/down_32.png")));

    jSeparator2.setOrientation(javax.swing.SwingConstants.VERTICAL);

    jLabel1.setText(com.floreantpos.POSConstants.NAME + ":");

    jLabel2.setText(com.floreantpos.POSConstants.NUMBER);

    jLabel3.setText(com.floreantpos.POSConstants.TYPE + ":");

    jLabel4.setText(com.floreantpos.POSConstants.COUPON_VALUE + ":");

    tfName.setEditable(false);

    tfNumber.setEditable(false);

    tfType.setEditable(false);

    tfValue.setEditable(false);

    btnEditValue.setText(com.floreantpos.POSConstants.GET_VALUE);
    btnEditValue.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        doEnterValue(evt);
      }
    });

    jLabel5.setFont(new java.awt.Font("Tahoma", 1, 18));
    jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    jLabel5.setText(com.floreantpos.POSConstants.TOTAL_DISCOUNT);

    lblTotalDiscount.setFont(new java.awt.Font("Tahoma", 1, 18));
    lblTotalDiscount.setForeground(new java.awt.Color(204, 51, 0));
    lblTotalDiscount.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    lblTotalDiscount.setText("jLabel6");

    org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                    .addContainerGap()
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jSeparator1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 642, Short.MAX_VALUE)
                            .add(titlePanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 642, Short.MAX_VALUE)
                            .add(layout.createSequentialGroup()
                                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 216, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                            .add(btnUp, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                            .add(btnDown, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                    .add(jSeparator2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                                    .add(btnOk, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 114, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                                    .add(btnCancel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 117, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                            .add(layout.createSequentialGroup()
                                                    .add(17, 17, 17)
                                                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                                            .add(jLabel4)
                                                            .add(jLabel3)
                                                            .add(jLabel2)
                                                            .add(jLabel1))
                                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                                            .add(layout.createSequentialGroup()
                                                                    .add(tfValue, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE)
                                                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                                                    .add(btnEditValue))
                                                            .add(tfType, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 225, Short.MAX_VALUE)
                                                            .add(tfNumber, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 225, Short.MAX_VALUE)
                                                            .add(tfName, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 225, Short.MAX_VALUE)))
                                            .add(layout.createSequentialGroup()
                                                    .add(18, 18, 18)
                                                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                                            .add(jLabel5, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE)
                                                            .add(jSeparator3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE)
                                                            .add(lblTotalDiscount, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE))))))
                    .addContainerGap())
    );
    layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                    .addContainerGap()
                    .add(titlePanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 323, Short.MAX_VALUE)
                            .add(jSeparator2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 323, Short.MAX_VALUE)
                            .add(layout.createSequentialGroup()
                                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                                            .add(jLabel1)
                                            .add(tfName, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                                            .add(jLabel2)
                                            .add(tfNumber, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                                            .add(jLabel3)
                                            .add(tfType, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                                            .add(jLabel4)
                                            .add(btnEditValue)
                                            .add(tfValue, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                    .add(jSeparator3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 10, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                            .add(layout.createSequentialGroup()
                                                    .add(5, 5, 5)
                                                    .add(btnUp, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                                    .add(btnDown, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                            .add(layout.createSequentialGroup()
                                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                                    .add(jLabel5)
                                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                                    .add(lblTotalDiscount)))))
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(jSeparator1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 10, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(btnCancel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(btnOk, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap())
    );

    pack();
  }

  public TicketCouponAndDiscount getSelectedCoupon() {
    try {
      double parseDouble = numberFormat.parse(tfValue.getText()).doubleValue();
      ticketCoupon.setValue(parseDouble);
    } catch (Exception x) {
      throw new PosException(com.floreantpos.POSConstants.INVALID_DISCOUNT_AMOUNT);
    }
    return ticketCoupon;
  }

  private void doEnterValue(java.awt.event.ActionEvent evt) {
    NumberSelectionDialog2 dialog = new NumberSelectionDialog2();
    dialog.setFloatingPoint(true);
    dialog.setTitle(com.floreantpos.POSConstants.DISCOUNT_AMOUNT);
    dialog.pack();
    dialog.open();

    if (!dialog.isCanceled()) {
      double value = dialog.getValue();
      tfValue.setText(Application.formatNumber(value));
      if (value == 0) {
        POSMessageDialog.showError(this, com.floreantpos.POSConstants.GET_DISCOUNT_AMOUNT);
        doEnterValue(evt);
      }
    }
  }

  private void doOk(java.awt.event.ActionEvent evt) {
    try {
      TicketCouponAndDiscount selectedCoupon = getSelectedCoupon();
      if (selectedCoupon == null) {
        POSMessageDialog.showError(this, com.floreantpos.POSConstants.SELECT_DISCOUNT);
        return;
      }
      if (selectedCoupon.getValue() == 0) {
        POSMessageDialog.showError(this, com.floreantpos.POSConstants.GET_DISCOUNT_AMOUNT);
        return;
      }
      setCanceled(false);
      dispose();
    } catch (PosException e) {
      POSMessageDialog.showError(this, e.getMessage());
    }
  }

  private void doCancel(java.awt.event.ActionEvent evt) {
    setCanceled(true);
    dispose();
  }

  public void initData() throws Exception {
    if (ticket.isPaymentTicket()) {
      couponList = null;
    } else {
      CouponAndDiscountDAO dao = new CouponAndDiscountDAO();
      couponList = dao.getValidCoupons();
    }
    listCoupons.setModel(new CouponListModel());
  }

  public void actionPerformed(ActionEvent e) {
    if ("scrollUP".equals(e.getActionCommand())) {
      if (couponList == null || couponList.size() == 0) {
        return;
      }

      int selectedRow = listCoupons.getSelectedIndex();

      if (selectedRow <= 0) {
        selectedRow = 0;
      } else {
        --selectedRow;
      }

      listCoupons.setSelectedIndex(selectedRow);
      Rectangle cellRect = listCoupons.getCellBounds(selectedRow, selectedRow);
      listCoupons.scrollRectToVisible(cellRect);
    } else if ("scrollDown".equals(e.getActionCommand())) {
      if (couponList == null || couponList.size() == 0) {
        return;
      }

      int selectedRow = listCoupons.getSelectedIndex();

      if (selectedRow < 0) {
        selectedRow = 0;
      } else if (selectedRow >= couponList.size() - 1) {

      } else {
        ++selectedRow;
      }

      listCoupons.setSelectedIndex(selectedRow);
      Rectangle cellRect = listCoupons.getCellBounds(selectedRow, selectedRow);
      cellRect.y += 20;
      listCoupons.scrollRectToVisible(cellRect);
    }
  }

  public void updateCouponView(CouponAndDiscount coupon) {
    if (coupon == null) {
      tfName.setText("");
      tfNumber.setText("");
      tfType.setText("");
      tfValue.setText("");
      return;
    }

    btnEditValue.setEnabled(false);

    tfName.setText(coupon.getName());
    if (coupon.getType() == CouponAndDiscount.FREE_AMOUNT) {
      btnEditValue.setEnabled(true);
    }

    tfNumber.setText(String.valueOf(coupon.getId()));
    tfType.setText(CouponAndDiscount.COUPON_TYPE_NAMES[coupon.getType()]);
    tfValue.setText(Application.formatNumber(coupon.getValue()));

    double totalDiscount = 0;
    double subtotal = ticket.getSubtotalAmount();

    ticketCoupon.setCouponAndDiscountId(coupon.getId());
    ticketCoupon.setName(coupon.getName());
    ticketCoupon.setType(coupon.getType());
    ticketCoupon.setValue(coupon.getValue());

    totalDiscount = ticket.calculateDiscountFromType(ticketCoupon, subtotal);
    ticketCoupon.setValue(totalDiscount);

    lblTotalDiscount.setText(Application.formatNumber(totalDiscount));
  }

  public void valueChanged(ListSelectionEvent e) {
    CouponAndDiscount coupon = (CouponAndDiscount) listCoupons.getSelectedValue();
    updateCouponView(coupon);
  }

  public Ticket getTicket() {
    return ticket;
  }

  public void setTicket(Ticket ticket) {
    this.ticket = ticket;
  }

  class CouponListModel extends AbstractListModel {

    public int getSize() {
      if (couponList == null) {
        return 0;
      }

      return couponList.size();
    }

    public Object getElementAt(int index) {
      return couponList.get(index);
    }

  }

  class CouponListRenderer extends DefaultListCellRenderer {

    @Override
    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
      CouponAndDiscount coupon = (CouponAndDiscount) value;
      return super.getListCellRendererComponent(list, coupon.getName(), index, isSelected, cellHasFocus);
    }
  }

  private com.floreantpos.swing.PosButton btnCancel;
  private com.floreantpos.swing.PosButton btnDown;
  private javax.swing.JButton btnEditValue;
  private com.floreantpos.swing.PosButton btnOk;
  private com.floreantpos.swing.PosButton btnUp;
  private javax.swing.JLabel jLabel1;
  private javax.swing.JLabel jLabel2;
  private javax.swing.JLabel jLabel3;
  private javax.swing.JLabel jLabel4;
  private javax.swing.JLabel jLabel5;
  private javax.swing.JScrollPane jScrollPane1;
  private javax.swing.JSeparator jSeparator1;
  private javax.swing.JSeparator jSeparator2;
  private javax.swing.JSeparator jSeparator3;
  private javax.swing.JLabel lblTotalDiscount;
  private javax.swing.JList listCoupons;
  private javax.swing.JTextField tfName;
  private javax.swing.JTextField tfNumber;
  private javax.swing.JTextField tfType;
  private javax.swing.JTextField tfValue;
  private com.floreantpos.ui.TitlePanel titlePanel1;

}
